<?php

namespace app\yunda\validate;
use think\Validate;
use think\Db;

/**
 * 寄快件验证
 * @package app\yunda\validate
 */
class Order extends Validate
{
    //定义验证规则
    protected $rule = [
        'openid|微信id' => 'require|checkOpenid',
        'get_id|收件地址'  => 'require|checkAddress',
        'send_id|发件地址'  => 'require|checkAddress1',
        'order_type|快件类型'  => 'require|checkType',

    ];

    //定义验证提示
    protected $message = [
    ];

    protected function checkOpenid($value,$rule)
    {
        $row=Db::table('dp_yd_user')->where('openid',$value)->find();
        if(empty($row)){
            return $rule='用户不存在';
        }
        return $rule=true;
    }

    protected function checkAddress($value,$rule,$data)
    {
        $row=Db::table('dp_yd_address')->where(['openid'=>$data['openid'],'id'=>$value,'type'=>2])->find();//收件
        if(empty($row)) return $rule='收件地址不存在';
        return $rule=true;
    }

    protected function checkAddress1($value,$rule,$data)
    {
        $row=Db::table('dp_yd_address')->where(['openid'=>$data['openid'],'id'=>$value,'type'=>1])->find();//发件
        if(empty($row)) return $rule='发件地址不存在';
        return $rule=true;
    }

    protected function checkType($value,$rule,$data)
    {
        $row=Db::table('dp_yd_category')->where('openid',$value)->find();
        if(empty($row)){
            return $rule='快件类型不存在';
        }
        return $rule=true;
    }
}
